package com.wfcm.controller;

import com.alibaba.fastjson.JSONObject;
import com.wfcm.annotation.IgnoreSign;
import com.wfcm.annotation.IgnoreToken;
import com.wfcm.entity.WfWxMenuEntity;
import com.wfcm.entity.WfWxShareEntity;
import com.wfcm.service.WfWxMenuService;
import com.wfcm.service.WfWxShareService;
import com.wfcm.utils.FastJSONUtils;
import com.wfcm.utils.HttpUtil;
import com.wfcm.wxUitls.ClickButton;
import com.wfcm.wxUitls.ViewButton;
import com.wfcm.wxUitls.WxButton;
import com.wfcm.wxUitls.WxMenu;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class WxMenuController {

    Logger logger = LoggerFactory.getLogger(WxMenuController.class);

    private static String  CREATE_MENU_URL=  "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";

    @Autowired
    private WfWxShareService wxShareService;
    @Autowired
    private WfWxMenuService wxMenuService;

   @RequestMapping("/setMenu")
   @IgnoreToken
   @IgnoreSign
   @ResponseBody
    public String setMenu(){
       WfWxShareEntity accessToken  = wxShareService.queryObject(1);
       JSONObject jsonObj= FastJSONUtils.getJSONObject(accessToken.getContent());
       String wxAccessToken = jsonObj.getString("access_token");
       logger.info("wxAccessToken:"+wxAccessToken);

       Map map = new HashMap<String,Object>();
       map.put("belong",1);
       List<WfWxMenuEntity> list1 = wxMenuService.queryList(map);
       map.put("belong",2);
       List<WfWxMenuEntity> list2 = wxMenuService.queryList(map);
       map.put("belong",3);
       List<WfWxMenuEntity> list3 = wxMenuService.queryList(map);

       String menu = JSONObject.toJSON(initMenu(list1,list2,list3)).toString();
       logger.info("自定义菜单内容"+menu);
       //System.out.println("自定义菜单内容"+menu);
       String result ="";
       try {
           result = createMenu(wxAccessToken, menu);
       } catch (IOException e) {
           logger.error("微信设置自定义菜单，请求失败");
           e.printStackTrace();
       }
        return  result;
    }

    public static String createMenu(String token,String menu) throws IOException {

        String result = "";
        String url = CREATE_MENU_URL.replace("ACCESS_TOKEN", token);
        String jsonObject = HttpUtil.jsonPost(url, menu);
        if(jsonObject != null){
            //result = jsonObject.getInt("errcode");
            System.out.println("结果：********************"+ jsonObject);
        }
        return result;
    }

    /**
     * 组装菜单
     * @return
     */
    public static WxMenu initMenu(List<WfWxMenuEntity> list1,List<WfWxMenuEntity> list2,List<WfWxMenuEntity> list3){

        // 动态初始化
        WxButton oneArray[] = new WxButton[list1.size()];//各种课程
        for (int i = 0; i <list1.size() ; i++) {
            if(list1.get(i).getType()==1) {
                ViewButton button = new ViewButton();
                button.setName(list1.get(i).getName());
                button.setType("view");
                button.setUrl(list1.get(i).getUrl());
                oneArray[i] = button;
            } else if(list1.get(i).getType()==2) {
                ClickButton button = new ClickButton();
                button.setName(list1.get(i).getName());
                button.setType("click");
                button.setKey(list1.get(i).getKey());
                oneArray[i] = button;
            }
        }
       /* WxButton twoArray[] = new WxButton[list2.size()];//个人中心
        for (int i = 0; i <list2.size() ; i++) {
            if(list2.get(i).getType()==1) {
                ViewButton button = new ViewButton();
                button.setName(list2.get(i).getName());
                button.setType("view");
                button.setUrl(list2.get(i).getUrl());
                twoArray[i] = button;
            } else if(list2.get(i).getType()==2) {
                ClickButton button = new ClickButton();
                button.setName(list2.get(i).getName());
                button.setType("click");
                button.setKey(list2.get(i).getKey());
                twoArray[i] = button;
            }
        }*/

        WxButton threeArray[] = new WxButton[list3.size()];//王芳时间
        for (int i = 0; i <list3.size() ; i++) {
            if(list3.get(i).getType()==1) {
                ViewButton button = new ViewButton();
                button.setName(list3.get(i).getName());
                button.setType("view");
                button.setUrl(list3.get(i).getUrl());
                threeArray[i] = button;
            } else if(list3.get(i).getType()==2) {
                ClickButton button = new ClickButton();
                button.setName(list3.get(i).getName());
                button.setType("click");
                button.setKey(list3.get(i).getKey());
                threeArray[i] = button;
            }
        }

        WxMenu menu = new WxMenu();

        ViewButton button21 = new ViewButton();
        button21.setName("个人中心"); button21.setType("view"); button21.setUrl(list2.get(0).getUrl());

        WxButton button1 = new WxButton();
        button1.setName("各种课程"); //将11/12两个button作为二级菜单封装第一个一级菜单
        button1.setSub_button(oneArray);


        WxButton button3 = new WxButton();
        button3.setName("王芳时间"); //将11/12两个button作为二级菜单封装第一个一级菜单
        button3.setSub_button(threeArray);

        menu.setButton(new WxButton[]{button1,button21,button3});// 将31Button直接作为一级菜单
        return menu;

    }

}
